{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Used memory= 4 MB, Npts= 210000\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "2a8b7129ab714357918b4f095ceab46d",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Plot(antialias=3, axes=['x', 'y', 'z'], background_color=16777215, camera=[4.5, 4.5, 4.5, 0.0, 0.0, 0.0, 1.0, …"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "\"\"\"\n",
    "Sierpinski3d fractal\n",
    "\"\"\"\n",
    "# Credits: K3D authors at:\n",
    "# https://github.com/K3D-tools/K3D-jupyter/tree/master/examples\n",
    "# https://simple.wikipedia.org/wiki/Sierpinski_triangle\n",
    "import numpy as np\n",
    "\n",
    "N = 10000\n",
    "x = np.zeros(N)\n",
    "y = np.zeros(N)\n",
    "z = np.zeros(N)\n",
    "\n",
    "x1 = np.empty_like(x)\n",
    "y1 = np.empty_like(y)\n",
    "z1 = np.empty_like(z)\n",
    "\n",
    "# Sierpinski triangle iterative functions\n",
    "def f1(x, y, z, x1, y1, z1, c):\n",
    "    x1[c] = 0.5*x[c]\n",
    "    y1[c] = 0.5*y[c]\n",
    "    z1[c] = 0.5*z[c]\n",
    "\n",
    "def f2(x, y, z, x1, y1, z1, c):\n",
    "    x1[c] = 0.5*x[c] + 1 / 2.0\n",
    "    y1[c] = 0.5*y[c]\n",
    "    z1[c] = 0.5*z[c]\n",
    "\n",
    "def f3(x, y, z, x1, y1, z1, c):\n",
    "    x1[c] = 0.5*x[c] + 1 / 4.0\n",
    "    y1[c] = 0.5*y[c] + np.sqrt(3) / 4\n",
    "    z1[c] = 0.5*z[c]\n",
    "\n",
    "def f4(x, y, z, x1, y1, z1, c):\n",
    "    x1[c] = 0.5*x[c] + 1 / 4.0\n",
    "    y1[c] = 0.5*y[c] + 1.0 / 4\n",
    "    z1[c] = 0.5*z[c] + np.sqrt(3) / 4\n",
    "\n",
    "\n",
    "functions = [f1, f2, f3, f4]\n",
    "probabilities = [1 / 4.0] * 4\n",
    "assert len(functions) == len(probabilities)\n",
    "\n",
    "X, Y, Z = x, y, z\n",
    "for i in range(20):\n",
    "    # pick indices for each function to be applied\n",
    "    r = np.random.choice(len(probabilities), size=N, p=probabilities)\n",
    "    for i, f in enumerate(functions):\n",
    "        f(x, y, z, x1, y1, z1, r == i)\n",
    "    x, x1 = x1, x\n",
    "    y, y1 = y1, y\n",
    "    z, z1 = z1, z\n",
    "    if i > 0:\n",
    "        X, Y, Z = np.hstack([X,x]), np.hstack([Y,y]), np.hstack([Z,z])\n",
    "\n",
    "# how much memory are we using, how many points there are\n",
    "print(\"Used memory=\", 3 * X.nbytes // 1024 ** 2, \"MB, Npts=\", X.shape[0])\n",
    "\n",
    "from vtkplotter import Points\n",
    "\n",
    "Points([X, Y, Z], r=0.1, c='blue').show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
